/**
 * 数据提交方式工具类，HttpServer Get or Post model
 * @author YichenZ
 */
package com.base.balibrary.utils.http;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import com.base.balibrary.base.BaseApplication;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.LinkedList;

public class HttpGetOrPost {

    private static final String ENCODE=HTTP.UTF_8;

    public static String getHttpGet() {
        return "";
    }

    /**
     * @param Url     地址
     * @param Content 参数内容
     * @return 返回数据
     * @throws UnsupportedEncodingException
     */
    public static String getHttpPost(String Url, String Content)
            throws UnsupportedEncodingException {
        BaseApplication.logMessage("http post:"+Content);
        return getHttpPost("application/json", ENCODE, Url, Content);
    }

    /**
     * @param ContentType application/json application/rdf+xml application/xml
     *                    text/html text/plain
     * @param Charset     编码格式
     * @param Url         地址
     * @param Content     参数内容
     * @return result     返回数据
     * @throws UnsupportedEncodingException
     */
    public static String getHttpPost(String ContentType, String Charset,
                                     String Url, String Content) throws UnsupportedEncodingException {
        String result = "";
        HttpPost request = BaseHttpClient.getInstance().getHttpDefaultPost(Url);
        request.setHeader("Accept", ContentType);
        request.setHeader("Content-type", ContentType);
        StringEntity entity = new StringEntity(Content, Charset);
        request.setEntity(entity);
        HttpClient httpClient = BaseHttpClient.getInstance().getHttpClient();
        HttpResponse response = null;
        try {
            response = httpClient.execute(request);
            result = EntityUtils.toString(response.getEntity());
            BaseApplication.logMessage("http post:"+result.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 表单提交方式上传数据
     *
     * @param Url
     * @param params
     * @return String
     * @throws java.io.UnsupportedEncodingException
     */
    public static String getHttpPostLin(String Url,
                                     LinkedList<NameValuePair> params)
            throws UnsupportedEncodingException {
        return getHttpPost("text/html", ENCODE, Url, params);
    }

    /**
     * @param ContentType 格式 application/json application/rdf+xml application/xml
     *                    text/html text/plain
     * @param Charset
     * @param Url
     * @return result 数据内容
     * @throws java.io.UnsupportedEncodingException
     */
    public static String getHttpPost(String ContentType, String Charset,
                                     String Url, LinkedList<NameValuePair> params)
            throws UnsupportedEncodingException {
        String result = "";
        if (params == null) {
            params = new LinkedList<NameValuePair>();
        }
        HttpPost request = new HttpPost(Url);
        request.setEntity(new UrlEncodedFormEntity(params, Charset));
        try {
            BaseApplication.logMessage(EntityUtils.toString(request.getEntity()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        HttpClient httpClient = BaseHttpClient.getInstance().getHttpClient();
        HttpResponse response = null;
        try {
            response = httpClient.execute(request);
            result = EntityUtils.toString(response.getEntity());
            BaseApplication.logMessage("http post:"+result.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

//    /**
//     * 上传图片
//     *
//     * @param Url 地址
//     * @param str 文件地址\名称
//     * @return
//     * @throws java.io.UnsupportedEncodingException
//     */
//    public static String getHttpImagePost(String Url, String str, int i)
//            throws UnsupportedEncodingException {
//        String BOUNDARY = "----WebKitFormBoundaryBeOEli5UBVXUoqD8";
//        String result = "";
//
//        HttpPost request = new HttpPost(Url);
//        MultipartEntity entity = new MultipartEntity(
//                HttpMultipartMode.BROWSER_COMPATIBLE, BOUNDARY, null);
//
//        File file = null;
//
//        file = new File(str);
//        if (file != null && file.exists()) {
//            if (i == 1) {
//                entity.addPart("secondhand_house_img", new FileBody(file));
//            } else {
//                entity.addPart("rent_house_img", new FileBody(file));
//            }
//        }
//
//        request.setEntity(entity);
//        HttpClient httpClient = BaseHttpClient.getInstance().getHttpClient();
//        HttpResponse response = null;
//        try {
//            response = httpClient.execute(request);
//            result = EntityUtils.toString(response.getEntity());
//            BaseApplication.logMessage("http post:"+result.toString());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return result;
//    }

}
